package com.lxp.tksys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.tksys.common.R;
import com.lxp.tksys.entity.Category;
import com.lxp.tksys.entity.Employee;
import com.lxp.tksys.service.CategoryService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        //log.info("category:{]",category);
        categoryService.save(category);         //直接新增
        return R.success("新增分类成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        //log.info("page={},pageSize={}",page,pageSize);

        Page<Category> pageInfo = new Page<>(page, pageSize);
        //条件构造器根据sort排序
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> delete(Long id){
        //log.info("删除id：{}",id);
        //categoryService.removeById(id);
        categoryService.remove(id);       //要查询是否有关联菜品/套餐,自定义remove方法
        return R.success("删除分类成功");
    }

    //修改分类信息
    @PutMapping
    public R<String> update(@RequestBody Category category){
        //log.info("修改分类信息:{}",category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }


    //获取菜品分类信息，显示在下拉框里面（type=1菜品分类,2套餐分类)-------用于回显数据
    @GetMapping("/list")
    public R<List<Category>> list(Category category){

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());  //根据（type=1菜品分类,2套餐分类)
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }


}
